package com.ken.callblock2.tools;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import android.os.Handler;
import android.os.Message;


/**
 * 模擬項server撈資料，時間可以自己設定，也可以用Handler接收or AfterThreadListener來收。
 * @author jason.chiang
 *
 */
public class TestConnectionEvent {
	
	private static TestConnectionEvent testConnectionEvent;
	
	private int sleepTime = 1000;
	
	private final static int MAX_CONNECTION = 5; // retry count;
	private Executor mConnectionPool 		= Executors.newFixedThreadPool(MAX_CONNECTION); // for General Connection
	
	protected Handler mHandler;	
	protected AfterThreadListener mListener;
	
	
	public void setSleepTime(int time){
		this.sleepTime = time;
	}
	
	public void setHandler(Handler handler){
		this.mHandler = handler;
	}
	
	public void setListener(AfterThreadListener listener){
		this.mListener = listener;
	}
	
	public void getSomthing_withHandler(){
		mConnectionPool.execute(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					Thread.sleep(sleepTime);
					
					if(mHandler!=null){
						Message msg = Message.obtain(mHandler);
						//msg.what = 0;
						//msg.obj = 0;
						//Bundle bundler = new Bundle();
						//msg.setData(bundler);
						mHandler.sendMessage(msg);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}
	
	
	public void getSomthing_withListener(){
		mConnectionPool.execute(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					Thread.sleep(sleepTime);
					
					if(mListener!=null){
						mListener.afterThread();
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}
	
	
	public interface AfterThreadListener{
		public void afterThread();
	}
}
